(function() {
	
	if (!window.CJ) {
		window.CJ = {};
	}


 /**
 * @class Horizon Class는 Key Event 를 처리한다.
 * @constructor Horizon
 * @description EventCtl Class 의 가상 메서드를  Overriding 하여 자식 객체만의 처리를 할 수 있다.
 */
	function Horizon () {}
	Horizon.prototype = new EventCtl();
	Horizon.prototype.left = function () {
		this.moveH('prev');
	};
	Horizon.prototype.right = function (evt) {
		this.moveH('next');
	};
	Horizon.prototype.entryList = function () {
		this.extend();
		var now = $("li.select", this.rootElem).addClass('select focus');
		this.pageNum(now.index());
	};
	Horizon.prototype.down = function() {
	};
	Horizon.prototype.up = function(){
	};
	Horizon.prototype.moveH = function (downup) {
		this.extend();
		var now = $("li.select", this.rootElem);
		var next = now[downup]();
		if (next.length>0) {
			var idx = next.index();
			var pos = Math.abs(next[0].offsetLeft) + 11;
			var rangeStart = Math.abs(this.rootElem.css('left').replace('px', ''));
			var rangeEnd = rangeStart + this.rootElem.parent().width() + (this.moveDistance/2);
			if (rangeStart <= pos && pos < rangeEnd)  {
				$("li", this.rootElem).removeClass('select focus');
				next.addClass('select focus');
				this.pageNum(idx);
			} else {
				$("li", this.rootElem).removeClass('focus');
				$(this.rootID + ' ' + (downup == 'next' ? '.btn_next' : '.btn_prev')).addClass('focus');
			}
		} else {
			$("li", this.rootElem).removeClass('focus');
			$(this.rootID + ' ' + (downup == 'next' ? '.btn_next' : '.btn_prev')).addClass('focus');
		}
	};
	Horizon.prototype.setFocus = function (elem) {
		var idx = $(elem).siblings().removeClass('select').end().addClass('select').index();
		this.paging(idx);
		this.pageNum(idx);
	};
	Horizon.prototype.pageNum = function(idx) {
		var obj = CJ.getStaticObject(this.name);
		var total = obj.totalCount;
		$(this.rootID+' .btn>span').text(parseInt(idx+1)+'/'+total);
	};
	Horizon.prototype.paging = function (idx) {
		var obj = CJ.getStaticObject(this.name);
		var total = obj.totalCount;
		var pos = this.moveDistance * idx;//(idx < 2 ? 0 : (idx-1));
		if (idx < total)  {
			this.rootElem.css({left : pos});
		}
		this.pageNum(idx);
	};
	Horizon.prototype.pageUp = function() {
		this.extend();
		var now = $("li.select", this.rootElem);
		var idx = now.index();
		var pageIdx = idx - this.pageUnitCount;
		var selIdx, moveIdx;
		if ( pageIdx < 0 ) {
			selIdx = 1;
			moveIdx = 0;
		} else {
			selIdx = pageIdx + 1;
			moveIdx = Math.floor(pageIdx/this.pageUnitCount) * this.pageUnitCount;
		}
		now.removeClass('select focus');
		$('li:nth-child('+selIdx+')', this.rootElem).addClass('select');
		this.paging(moveIdx);
	};
	Horizon.prototype.pageDown = function(){
		this.extend();
		var obj = CJ.getStaticObject(this.name);
		var total = obj.totalCount;
		var now = $("li.select", this.rootElem);
		var idx = now.index();
		var pageIdx = idx + this.pageUnitCount;
		var selIdx, moveIdx;
		if ( pageIdx < total ) {
			selIdx = pageIdx + 1;
			moveIdx = Math.floor(pageIdx/this.pageUnitCount) * this.pageUnitCount;
		} else {
			selIdx = total;
			moveIdx = Math.floor((total - 1)/this.pageUnitCount) * this.pageUnitCount;
		}
		now.removeClass('select focus');
		$('li:nth-child('+selIdx+')', this.rootElem).addClass('select');
		this.paging(moveIdx);
	};
	Horizon.prototype.bindEvent = function () {
		var self = this;
		$(this.rootElem)
		.on("click", "li", function(evt) {
			// if (self.moveDistance) {
				// var idx = $(this).index();
				// var pos = self.moveDistance * (idx < 2 ? 0 : (idx-1));
				// if (idx+2 < $('li', this.parentElement).length)  {
					// $(this.parentElement).css({left : pos});
				// }
			// }
			$(this).siblings().removeClass("select focus").end().addClass("select focus");
			self.enter(evt);
		});
	};
	CJ.Horizon = Horizon;
	
})();